package com.ysd.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.ysd.entity.ProductCategory;
import com.ysd.util.DBManager;

// 实现类
public class ProductCategoryDaoImpl {
	
	// 查询所有产品分类
	// 访问修饰符 返回值类型 方法名(参数列表){方法体}
	public List<ProductCategory> getAllProductCategories(){
		List<ProductCategory> pcList = new ArrayList<ProductCategory>();//定义一个ArrayList
		
		Connection conn = DBManager.getConnection(); //获得连接
		Statement st = null;
		ResultSet rs = null;
		
		try { //正常情况
			st = conn.createStatement(); // 创建statement对象, 这个对象的作用是执行sql语句
			rs = st.executeQuery("select * from productcategory"); //执行查询的sql语句
			while (rs.next()) { // 遍历结果集
				int pcId = rs.getInt("ProductCategoryId");
				String pcName = rs.getString("ProductCategoryName");
				ProductCategory pc = new ProductCategory(pcId, pcName);
				//The constructor ProductCategory(String, String) is undefined
				pcList.add(pc);
			}
			return pcList;
		} catch (SQLException e) { //异常情况
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);//关闭连接
		}
	}
	
	/**
	 * 分页查询
	 * @return
	 */
	public List<ProductCategory> getProductCategoriesByPage(int page, int size){
		List<ProductCategory> pcList = new ArrayList<ProductCategory>();//定义一个ArrayList
		
		Connection conn = DBManager.getConnection(); //获得连接
		Statement st = null;
		ResultSet rs = null;
		
		try { //正常情况
			st = conn.createStatement(); // 创建statement对象, 这个对象的作用是执行sql语句
			//String sql = "select * from productcategory limit "+(page-1)*size+", "+size;
			//String sql = "select * from productcategory limit ("+page+"-1)*"+size+", "+size;
			String sql = "select * from productcategory limit "+page+", "+size;
			System.out.println(sql);
			rs = st.executeQuery(sql); //执行查询的sql语句
			while (rs.next()) { // 遍历结果集
				int pcId = rs.getInt("ProductCategoryId");
				String pcName = rs.getString("ProductCategoryName");
				ProductCategory pc = new ProductCategory(pcId, pcName);
				//The constructor ProductCategory(String, String) is undefined
				pcList.add(pc);
			}
			return pcList;
		} catch (SQLException e) { //异常情况
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);//关闭连接
		}
	}
	
	/**
	 * 根据分类id删除产品分类
	 * @param cId 要删除的产品分类的id
	 * @return 数据库受影响的行数
	 */
	public int deleteProductCateById(int cId) {
		return DBManager.executeUpdate("delete from productcategory where productcategoryid="+cId);
	}
	
	/**
	 * 添加产品分类
	 * @param pc
	 * @return 数据库受影响的行数
	 */
	public int addProductCategory(ProductCategory pc) {//[productCategoryId=0, productCategoryName=tongjintao]
		return DBManager.executeUpdate("insert into productcategory values(null,'"+pc.getProductCategoryName()+"')");
	}
	
	/**
	 * 根据名称查询产品分类
	 * @param name
	 * @return
	 */
	public List<ProductCategory> getProductCategoryByName(String name){
		List<ProductCategory> pcList = new ArrayList<ProductCategory>();
		
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			rs = st.executeQuery("select * from productcategory where productCategoryName='"+name+"'");
			while(rs.next()) {
				int pcId = rs.getInt(1);
				String pcName = rs.getString(2);
				ProductCategory pc = new ProductCategory(pcId, pcName);
				pcList.add(pc);
			}
			return pcList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	
	// 返回List是因为查询的结果可能是多条
	// 返回单个对象是 
	
	public ProductCategory getProductCategoryById(int id) {
		ProductCategory pc = null;
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			rs = st.executeQuery("select * from productcategory where productcategoryId=" + id);
			while(rs.next()) {
				int pcId = rs.getInt(1);
				String pcName = rs.getString(2);
				pc = new ProductCategory(pcId, pcName);
			}
			return pc;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
		
	}
	
	/**
	 * 修改产品分类
	 * @param pc
	 * @return 返回受影响行数
	 */
	public int updateProductCategory(ProductCategory pc) {
		String sql = "update productcategory set ProductCategoryName='"+pc.getProductCategoryName()+"' where ProductCategoryId=" + pc.getProductCategoryId();
		System.out.println(sql);
		return DBManager.executeUpdate(sql);
	}
	
}









